package hot;

import java.util.HashMap;
import java.util.Map;

public class findDuplicate {
    public int findDuplicate2(int[] nums) {
        int ret = 0;
        int n = nums.length;
        int l = 0;
        int r = n-1;
        Map<Integer, Integer> hash = new HashMap<>();
        while(l <= r) {
            int mid = l + (r-l)/2;
            int cnt = 0;
            for(int i = 0; i < n;i++) {
                if(nums[i] <= mid) {
                    cnt++;
                }
            }
            if(cnt <= mid) {
                l = mid+1;
            }else {
                r = mid -1;
                ret = mid;
            }
        }
        return ret;
    }
    public int findDuplicate(int[] nums) {
        int ret = 0;
        Map<Integer, Integer> hash = new HashMap<>();
        for (int x : nums) {
            if (hash.getOrDefault(x, 0) != 0) {
                ret = x;
                break;
            }
            hash.put(x, hash.getOrDefault(x, 0) + 1);
        }
        return ret;
    }
}
